package org.example.config;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.example.domain.po.ErpMenu;
import org.example.domain.po.ErpUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {

    private ErpUser users;
    private List<ErpMenu> menus;

    public LoginUser(ErpUser users, List<ErpMenu> menus) {
        this.users = users;
        this.menus = menus;
    }


    //将菜单信息转换成权限列表信息
    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //Menus转换成GrantedAuthority
        List<GrantedAuthority> authorities = menus.stream()
                .map(item ->
                        new SimpleGrantedAuthority
                                (item.getPerm()))
                .collect(Collectors.toList());
        return authorities;
    }

    @Override
    @JsonIgnore
    public String getPassword() {
        return users.getPassword();
    }

    @Override
    @JsonIgnore
    public String getUsername() {
        return users.getUsername();
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return true;
    }
}